﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

namespace Maticsoft.Web
{
    public partial class Header : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetMenu();
        }

        protected void GetMenu()
        {
            BLL.Channel bll = new BLL.Channel();
            DataSet ds = bll.GetList("parentId=1 order by OrderNo asc");
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("<ul>");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (HasSon(int.Parse(ds.Tables[0].Rows[i]["ChannelId"].ToString())))
                {
                    strBuilder.Append("<li><a href=\"javascript:void(0);\" class=\"parent\"><span>" + ds.Tables[0].Rows[i]["ChannelName"] + "</span></a>");
                    DataSet dss = bll.GetList("parentId=" + ds.Tables[0].Rows[i]["ChannelId"].ToString() + "  order by OrderNo asc");
                    strBuilder.Append("<ul>");
                    foreach (DataRow dr in dss.Tables[0].Rows)
                    {
                        strBuilder.Append("<li><a href=\"list.aspx?channelId=1&classId=" + dr["ChannelId"].ToString() + "\"><span>" + dr["ChannelName"] + "</span></a></li>");
                    }
                    strBuilder.Append("</ul>");
                    strBuilder.Append("</li>");
                }
                else
                {
                    strBuilder.Append("<li><a href=\"list.aspx?channelId=1&classId=" + ds.Tables[0].Rows[i]["ChannelId"].ToString() + "\"><span>" + ds.Tables[0].Rows[i]["ChannelName"] + "</span></a></li>");
                }
            }
            strBuilder.Append("</ul>");
            ltlMenu1.Text = strBuilder.ToString();

            strBuilder = new StringBuilder();
            ds = bll.GetList("parentId=2 order by OrderNo asc");
            strBuilder.Append("<ul>");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (HasSon(int.Parse(ds.Tables[0].Rows[i]["ChannelId"].ToString())))
                {
                    strBuilder.Append("<li><a href=\"javascript:void(0);\" class=\"parent\"><span>" + ds.Tables[0].Rows[i]["ChannelName"] + "</span></a>");
                    DataSet dss = bll.GetList("parentId=" + ds.Tables[0].Rows[i]["ChannelId"].ToString() + "  order by OrderNo asc");
                    strBuilder.Append("<ul>");
                    foreach (DataRow dr in dss.Tables[0].Rows)
                    {
                        strBuilder.Append("<li><a href=\"list.aspx?channelId=2&classId=" + dr["ChannelId"].ToString() + "\"><span>" + dr["ChannelName"] + "</span></a></li>");
                    }
                    strBuilder.Append("</ul>");
                    strBuilder.Append("</li>");
                }
                else
                {
                    strBuilder.Append("<li><a href=\"list.aspx?channelId=2&classId=" + ds.Tables[0].Rows[i]["ChannelId"].ToString() + "\"><span>" + ds.Tables[0].Rows[i]["ChannelName"] + "</span></a></li>");
                }
            }
            strBuilder.Append("</ul>");
            ltlMenu2.Text = strBuilder.ToString();

            strBuilder = new StringBuilder();
            ds = bll.GetList("parentId=45 order by OrderNo asc");
            strBuilder.Append("<ul>");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (HasSon(int.Parse(ds.Tables[0].Rows[i]["ChannelId"].ToString())))
                {
                    strBuilder.Append("<li><a href=\"javascript:void(0);\" class=\"parent\"><span>" + ds.Tables[0].Rows[i]["ChannelName"] + "</span></a>");
                    DataSet dss = bll.GetList("parentId=" + ds.Tables[0].Rows[i]["ChannelId"].ToString() + "  order by OrderNo asc");
                    strBuilder.Append("<ul>");
                    foreach (DataRow dr in dss.Tables[0].Rows)
                    {
                        strBuilder.Append("<li><a href=\"list.aspx?channelId=45&classId=" + dr["ChannelId"].ToString() + "\"><span>" + dr["ChannelName"] + "</span></a></li>");
                    }
                    strBuilder.Append("</ul>");
                    strBuilder.Append("</li>");
                }
                else
                {
                    strBuilder.Append("<li><a href=\"list.aspx?channelId=45&classId=" + ds.Tables[0].Rows[i]["ChannelId"].ToString() + "\"><span>" + ds.Tables[0].Rows[i]["ChannelName"] + "</span></a></li>");
                }
            }
            strBuilder.Append("</ul>");
            ltlMenu3.Text = strBuilder.ToString();
        }

        protected bool HasSon(int id)
        {
            BLL.Channel bll = new BLL.Channel();
            DataSet ds = bll.GetList("parentId=" + id);
            if (ds.Tables[0].Rows.Count > 0)
                return true;
            else
                return false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Search.aspx?key=" + key.Value);
        }
    }
}